package edu.nf.project.webs.storeOperation;

import edu.nf.project.entitys.dto.storeOperation.CargoDTO;
import edu.nf.project.entitys.entity.storeOperation.convention.cargo.Cargo;
import edu.nf.project.entitys.entity.storeOperation.convention.cargo.CargoOrder;
import edu.nf.project.services.exception.derivedAnomalies.CustomerOrderException;
import edu.nf.project.services.response.BaseController;
import edu.nf.project.services.response.ResultVO;
import edu.nf.project.services.storeOperation.CargoOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 货物订单控制器
 */
@RestController
@RequestMapping("/api/cargo")
public class CargoOrderController extends BaseController {
    @Autowired
    private CargoOrderService cargoOrderService;

    /**
     * 获取全部货物订单信息
     */
    @GetMapping("/all")
    public ResultVO<List<CargoOrder>> getAllCargoOrder() {
        try {
            List<CargoOrder> cargoOrders = cargoOrderService.getAllCargoOrder();
            return success(cargoOrders);
        } catch (Exception e) {
            if (e instanceof CustomerOrderException) {
                throw e;
            }
            throw new CustomerOrderException(80000, "系统错误，请稍后重试");
        }
    }

    /**
     * 获取货物订单详细相关信息
     */
    @GetMapping("/one")
    public ResultVO<CargoDTO> getCargoOrder(@RequestParam Integer oId) {
        try {
            CargoDTO cargoDTO = cargoOrderService.getCargoOrder(oId);
            return success(cargoDTO);
        } catch (Exception e) {
            if (e instanceof CustomerOrderException) {
                throw e;
            }
            System.out.println(e);
            throw new CustomerOrderException(80000, "系统错误，请稍后重试");
        }
    }

    @GetMapping("/allCargo")
    public ResultVO<List<Cargo>> getAllCargo() {
        try {
            List<Cargo> cargos = cargoOrderService.getAllCargo();
            return success(cargos);
        } catch (Exception e) {
            if (e instanceof CustomerOrderException) {
                throw e;
            }
            System.out.println(e);
            throw new CustomerOrderException(80000, "系统错误，请稍后重试");
        }
    }

    @GetMapping("/addCargo")
    public ResultVO<Boolean> inventoryPlusOne(@RequestParam Integer cargoId, @RequestParam Integer quantity) {
        try {
            Boolean flag = cargoOrderService.increaseStock(cargoId, quantity);
            return success(flag);
        } catch (Exception e) {
            if (e instanceof CustomerOrderException) {
                throw e;
            }
            throw new CustomerOrderException(80000, "系统错误，请稍后重试");
        }
    }

    @GetMapping("/delCargo")
    public ResultVO<Boolean> inventoryMinusOne(@RequestParam Integer cargoId, @RequestParam Integer quantity) {
        try {
            Boolean flag = cargoOrderService.decreaseStock(cargoId, quantity);
            return success(flag);
        } catch (Exception e) {
            if (e instanceof CustomerOrderException) {
                throw e;
            }
            throw new CustomerOrderException(80000, "系统错误，请稍后重试");
        }
    }
}
